/**   
 * All rights Reserved, Designed By MiGu  
 * Copyright:    Copyright(C) 2016-2020  
 * Company       MiGu  Co., Ltd.
*/
package com.migu.spms.webapi;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.migu.spms.dao.entity.RequireTypeTreeCriteria;
import com.migu.spms.dao.entity.RequireTypeTreeDO;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IRequireTypeTreeService;
import com.migu.spms.utils.PropertiesCopyUtil;
import com.migu.spms.webapi.data.GetTypeTreeRequest;
import com.migu.spms.webapi.data.MoveRequireTypeTreeRequest;
import com.migu.spms.webapi.data.RequireTypeTreeRequest;
import com.migu.spms.webapi.data.RequireTypeTreeResponse;

/**   
* RequireTypeTreeController类
* 项目名称:  SPMS功能需求
* 包名称:    com.migu.spms.webapi   
* 类名称:    RequireTypeTreeController.java
* 类描述:    分类树
* 创建人:    wcr   
* 创建时间:  2017年6月19日 上午9:42:31   
*/
@Controller
@RequestMapping(value = "/requireType", produces = "application/json;charset=UTF-8")
public class RequireTypeTreeController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(RequireTypeTreeController.class);

    @Resource(name = "requireTypeTreeServiceImpl")
    private IRequireTypeTreeService requireTypeTreeService;

    /**
     * 分类列表树
     * 
     * @param req
     * @param resp
     * @param sessionId
     * @return
     * @exception @throws
     */
    @ResponseBody
    @RequestMapping(value = "/getTypeTree")
    public String getTypeTree(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
        int code = 200;
        User user = getUserBySession(req, sessionId);
        if (user == null) {
            return generateResponse(401);
        }
        GetTypeTreeRequest typeTreeRequest = ParseMsg(req, GetTypeTreeRequest.class);
        List<RequireTypeTreeDO> list = requireTypeTreeService.selectRequireTypeTreeList(typeTreeRequest);
        RequireTypeTreeResponse res = new RequireTypeTreeResponse();
        res.setRequireTypeTreeList(list);
        return generateResponse(res, code);
    }

    @ResponseBody
    @RequestMapping(value = "/addTypeTree")
    public String addTypeTree(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
        int code = 200;
        User user = getUserBySession(req, sessionId);
         if (user == null) {
         return generateResponse(401);
         }
        RequireTypeTreeRequest res = ParseMsg(req, RequireTypeTreeRequest.class);
        requireTypeTreeService.insertRequireTypeTree(res, user);
        return generateResponse(code);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteTypeTree")
    public String deleteTypeTree(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
        int code = 200;
        User user = getUserBySession(req, sessionId);
        // if (user == null) {
        // return generateResponse(401);
        // }
        RequireTypeTreeRequest res = ParseMsg(req, RequireTypeTreeRequest.class);
        requireTypeTreeService.deleteRequireTypeTree(res);
        return generateResponse(code);
    }

    @ResponseBody
    @RequestMapping(value = "/updateTypeTree")
    public String updateTypeTree(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
        int code = 200;
        User user = getUserBySession(req, sessionId);
        // if (user == null) {
        // return generateResponse(401);
        // }
        RequireTypeTreeRequest res = ParseMsg(req, RequireTypeTreeRequest.class);
        RequireTypeTreeCriteria criteria = PropertiesCopyUtil.parse(res, RequireTypeTreeCriteria.class);
        requireTypeTreeService.updateRequireTypeTree(criteria);
        return generateResponse(code);
    }

    @ResponseBody
    @RequestMapping(value = "/moveTypeTree")
    public String moveTypeTree(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
        int code = 200;
        User user = getUserBySession(req, sessionId);
        if (user == null) {
            return generateResponse(401);
        }
        MoveRequireTypeTreeRequest res = ParseMsg(req, MoveRequireTypeTreeRequest.class);
        requireTypeTreeService.moveTypeTreeByRequire(res, user);
        return generateResponse(code);
    }

}
